package ar.com.fiuba.domain.impl;

import java.util.List;

import ar.com.fiuba.domain.CoefficientCalculationStrategy;
import ar.com.fiuba.domain.Word;
import ar.com.fiuba.domain.api.Dictionary;

public class Corrector {

	Dictionary dictionary;

	CoefficientCalculationStrategy coefficientCalculator;

	public Corrector(Dictionary dictionary, CoefficientCalculationStrategy coefficientCalculator) {
		this.dictionary = dictionary;
		this.coefficientCalculator = coefficientCalculator;
	}

	public Word getBestMatch(Word wordToCorrect) {
		double maxCoefficient = 0;
		List<Word> wordsWithSameNgramms = dictionary.getWordsWithSameNGramms(wordToCorrect.getNgramms());

		Word wordWithMaxCoefficientWord = wordsWithSameNgramms.get(0);
		
		for (Word currentWord : wordsWithSameNgramms) {
			double coef = coefficientCalculator.calculate(wordToCorrect, currentWord);
			if (coef >= maxCoefficient) {
				maxCoefficient = coef;
				wordWithMaxCoefficientWord = currentWord;
			}
		}
		return wordWithMaxCoefficientWord;
	}

}
